package com.example.park.weixiu

import android.text.Editable
import android.text.TextWatcher
import android.view.MenuItem
import android.widget.PopupMenu
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.WeiXiuEntityItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.park.R
import com.example.park.databinding.ActivityWeixiuBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/park/WeixiuActivity")
@AndroidEntryPoint
class WeixiuActivity : BaseActivity<ActivityWeixiuBinding,WeiXiuViewModel>() {
    lateinit var weiXiuAdapter: WeiXiuAdapter
    lateinit var list :MutableList<WeiXiuEntityItem>
    var flag =false//true:升序 false:降序
    val newList= mutableListOf<WeiXiuEntityItem>()
    override fun initView() {
        weiXiuAdapter= WeiXiuAdapter()
        binding.wxRv.adapter=weiXiuAdapter
        binding.wxRv.layoutManager=LinearLayoutManager(this)
        vm.weixiu()
        binding.repx.setOnClickListener {
        //weiXiuAdapter.data.clear()
            if (flag){
                //newList.sortByDescending { it.repairTime }
                weiXiuAdapter.data.sortByDescending { it.repairTime }
                binding.repairOrderiv.setImageResource(R.drawable.baseline_arrow_upward_24)
            }else{
               // newList.sortBy { it.repairTime }
                weiXiuAdapter.data.sortBy { it.repairTime }
                binding.repairOrderiv.setImageResource(R.drawable.baseline_arrow_downward_24)
            }
          //  weiXiuAdapter.addData(newList)
            weiXiuAdapter.notifyDataSetChanged()
            flag=!flag
        }
        //点击创建维修
        binding.weixiuAdd.setOnClickListener {
            ARouter.getInstance().build("/park/AddWeiXiuActivity").navigation()
        }

        binding.wxSx.setOnClickListener {
            val popupMenu =PopupMenu(this,binding.wxSx)
            popupMenu.inflate(R.menu.pop_weixiu)
            popupMenu.setOnMenuItemClickListener(object :PopupMenu.OnMenuItemClickListener{
                override fun onMenuItemClick(p0: MenuItem?): Boolean {
                    when(p0?.itemId){
                        R.id.dcl->{
                            val data: MutableList<WeiXiuEntityItem> = mutableListOf()
                            list.forEach {
                                if (it.processingStatus=="待处理"){
                                    data.add(it)
                                }
                            }
                            weiXiuAdapter.data.clear()
                            weiXiuAdapter.data=data
                            weiXiuAdapter.notifyDataSetChanged()
                        }
                        R.id.clz->{
                            val data: MutableList<WeiXiuEntityItem> = mutableListOf()
                            list.forEach {
                                if (it.processingStatus=="处理中"){
                                    data.add(it)
                                }
                            }
                            weiXiuAdapter.data.clear()
                            weiXiuAdapter.data=data
                            weiXiuAdapter.notifyDataSetChanged()
                        }
                        R.id.ywc->{
                            val data: MutableList<WeiXiuEntityItem> = mutableListOf()
                            list.forEach {
                                if (it.processingStatus=="已完成"){
                                    data.add(it)
                                }
                            }
                            weiXiuAdapter.data.clear()
                            weiXiuAdapter.data=data
                            weiXiuAdapter.notifyDataSetChanged()
                        }
                    }
                    return true
                }
            })

            popupMenu.show()
        }
        val itemTouchHelper =ItemTouchHelper(object :
            ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
               return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.adapterPosition
                vm.delWeiXiuXq(position)
                weiXiuAdapter.notifyItemRemoved(position)
            }
        }).attachToRecyclerView(binding.wxRv)

//        binding.wxSearch.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
//            override fun onQueryTextSubmit(query: String?): Boolean {
//                if (query!=null){
//                    val data: MutableList<WeiXiuEntityItem> = mutableListOf()
//                    list.forEach {
//                        if (it.orderNumber==query){
//                            data.add(it)
//                        }
//                    }
//                    weiXiuAdapter.data.clear()
//                    weiXiuAdapter.data=data
//                    weiXiuAdapter.notifyDataSetChanged()
//                    vm.queryWeiXiu(query)
//                }else{
//                    val data: MutableList<WeiXiuEntityItem> = mutableListOf()
//                    list.forEach {
//                        if (it.repairName==query){
//                            data.add(it)
//                        }
//                    }
//                    weiXiuAdapter.data.clear()
//                    weiXiuAdapter.data=data
//                    weiXiuAdapter.notifyDataSetChanged()
//                    vm.queryRenWeiXiu(query!!)
//                }
//                return true
//            }
//
//            override fun onQueryTextChange(newText: String?): Boolean {
//                if (newText!=null){
//                    vm.weixiu()
//                }
//                return true
//            }
//
//        })

        binding.btnSearch.setOnClickListener {
            val context = binding.wxSearch.text.toString()
            if (context.startsWith("WX")){
                vm.queryWeiXiu(binding.wxSearch.text.toString())
            }else{
                vm.queryRenWeiXiu(binding.wxSearch.text.toString())
            }
        }
        //清空之后有列表
        binding.wxSearch.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
               if (binding.wxSearch.text.toString().isEmpty()){
                   vm.weixiu()
               }
            }

            override fun afterTextChanged(p0: Editable?) {

            }

        })
        //点击之后跳转到详情
        weiXiuAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/park/WeiXiuXqActivity").withString("wxId",
                weiXiuAdapter.data[position].id.toString()
            ).navigation()
        }
    }

    override fun initData() {
        vm.success.observe(this){
            list=it
            list.addAll(it)
            newList.clear()
            newList.addAll(it)
            weiXiuAdapter.data.clear()
            weiXiuAdapter.data.addAll(it)
            weiXiuAdapter.notifyDataSetChanged()

        }
        vm.querySuccess.observe(this){
            weiXiuAdapter.data.clear()
            weiXiuAdapter.addData(it)
            weiXiuAdapter.notifyDataSetChanged()
        }
        vm.delWeiXiuXqSuccess.observe(this){
            ToastUtils.showLong("删除成功")
        }
        vm.queryRenSuccess.observe(this){
//            list=it
//            list.addAll(it)
            weiXiuAdapter.data.clear()
            weiXiuAdapter.addData(it)
            weiXiuAdapter.notifyDataSetChanged()
        }
//        weiXiuAdapter.addChildClickViewIds(R.id.cb)
//        weiXiuAdapter.setOnItemChildClickListener { adapter, view, position ->
//            when(view.id){
//                R.id.cb->{
//                    weiXiuAdapter.data[position].isCheck=!weiXiuAdapter.data[position].isCheck
//                }
//            }
//            checkAll()
//        }
        weiXiuAdapter.addChildClickViewIds(R.id.cb)
        weiXiuAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.cb->{
                    weiXiuAdapter.data[position].isCheck=!weiXiuAdapter.data[position].isCheck
                }
            }
            checkAll()
        }
//        binding.wxAll.setOnCheckedChangeListener { compoundButton, b ->
//            binding.wxAll.setOnClickListener {
//                weiXiuAdapter.data.forEach {
//                    it.isCheck=b
//                }
//                weiXiuAdapter.notifyDataSetChanged()
//                checkAll()
//            }
//        }
        binding.wxAll.setOnCheckedChangeListener { compoundButton, b ->
            binding.wxAll.setOnClickListener {
                weiXiuAdapter.data.forEach {
                    it.isCheck=b
                }
                weiXiuAdapter.notifyDataSetChanged()
                checkAll()
            }
        }
    }

    private fun checkAll() {
//        val list= mutableListOf<Boolean>()
//        weiXiuAdapter.data.forEach {
//            list.add(it.isCheck)
//        }
//        if (list.size>0){
//            if (false in list){
//                binding.wxAll.isChecked =false
//            }else{
//                binding.wxAll.isChecked=true
//            }
//        }else{
//            binding.wxAll.isChecked=false
//        }

        val list = mutableListOf<Boolean>()
        weiXiuAdapter.data.forEach {
            list.add(it.isCheck)
        }
        if (list.size>0){
            if (false in list){
                binding.wxAll.isChecked=false
            }else{
                binding.wxAll.isChecked=true
            }
        }else{
            binding.wxAll.isChecked=false
        }
    }

    override fun onResume() {
        super.onResume()
        vm.weixiu()
    }

}